
#region Using Statements
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
#endregion

namespace OrigPacman
{
    public enum Direction { NORTH, SOUTH, EAST, WEST, STOP };

    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        ContentManager content;

        KeyPress.KeyPressGame keys;

        #region pacman data
        //Various variables
        Texture2D pacmanUp;
        Texture2D pacmanDown;
        Texture2D pacmanRight;
        Texture2D pacmanLeft;
        Texture2D currentTexture;
        Texture2D deathFrames;
        Direction currentDirection;
        float spriteX = 0;
        float spriteY = 0;
        int currentX = 0;
        float totalTime = 0.0f;
        bool adjustDistance = false;
        //Values used when big pellet is eaten
        bool superMode = false;
        float superModeTime = 7.0f;
        //====================================
        List<Waypoint> currentWaypoints = null;
        List<Ghost> currentGhostPositions = null;
        int nextWaypoint = 0;
        int prevWaypoint = 0;
        float length = 0.0f;
        int lives = 3;
        float frameTime = 0.25f;
        int currentDeathFrame = 0;
        bool playDeathAnimation = false;
        //Saves the current Key information
        Keys currentKey;
        //Used to log information
        #endregion

        #region ghost data
        List<Ghost> ghostList;
        List<Waypoint> wayPointList;
        Random rand;
        // Specify file, instructions, and privelegdes
        //FileStream file = new FileStream("logging.txt", FileMode.Create, FileAccess.Write);
        //StreamWriter sw;
        Waypoint tmpWay; //tmp variable for waypoints
        #endregion

        Map map;
        SpriteBatch sprite;

        #region game methods
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            content = new ContentManager(Services);

            keys = new KeyPress.KeyPressGame();

            map = new Map(graphics.GraphicsDevice);
            setCurrentPacmanWaypoints(map.getWaypoints());
            
          
            ghostList = new List<Ghost>();
            ghostList.Add(new Ghost(Ghost.Color.BLUE, new Vector2(100, 100)));
            ghostList.Add(new Ghost(Ghost.Color.GREEN, new Vector2(200, 200)));
            ghostList.Add(new Ghost(Ghost.Color.PURPLE, new Vector2(300, 300)));
            ghostList.Add(new Ghost(Ghost.Color.RED, new Vector2(400, 400)));

            wayPointList = new List<Waypoint>();
            rand = new Random();
            // Create a new stream to write to the file
            //sw = new StreamWriter(file);
            //sw.Write("Hello Testing");
            setWaypoints(map.getWaypoints());
        }


        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }


        /// <summary>
        /// Load your graphics content.  If loadAllContent is true, you should
        /// load content from both ResourceManagementMode pools.  Otherwise, just
        /// load ResourceManagementMode.Manual content.
        /// </summary>
        /// <param name="loadAllContent">Which type of content to load.</param>
        protected override void LoadGraphicsContent(bool loadAllContent)
        {
            if (loadAllContent)
            {
                sprite = new SpriteBatch(graphics.GraphicsDevice);
                // TODO: Load any ResourceManagementMode.Automatic content
                pacmanUp = content.Load<Texture2D>("images/PacmanUp");
                pacmanDown = content.Load<Texture2D>("images/PacmanDown");
                pacmanRight = content.Load<Texture2D>("images/PacmanRight");
                pacmanLeft = content.Load<Texture2D>("images/PacmanLeft");
                deathFrames = content.Load<Texture2D>("images/deathAnim");
                currentTexture = pacmanRight;
                map.LoadContent(content);
                foreach (Ghost g in ghostList)
                {
                    g.Texture = content.Load<Texture2D>("images/" + g.CurrentColor);
                }
            }

            // TODO: Load any ResourceManagementMode.Manual content
        }


        /// <summary>
        /// Unload your graphics content.  If unloadAllContent is true, you should
        /// unload content from both ResourceManagementMode pools.  Otherwise, just
        /// unload ResourceManagementMode.Manual content.  Manual content will get
        /// Disposed by the GraphicsDevice during a Reset.
        /// </summary>
        /// <param name="unloadAllContent">Which type of content to unload.</param>
        protected override void UnloadGraphicsContent(bool unloadAllContent)
        {
            if (unloadAllContent == true)
            {
                content.Unload();
            }
        }


        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the default game to exit on Xbox 360 and Windows
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here
            // The time since Update was called last
            float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
            KeyPress.KeyPressGame.UpdateInput(elapsed);
            if (wasEscapePressed() == false)
            {
                // TODO: Add your game logic here
                UpdateGhosts(elapsed, getPacmanPosition());
                setCurrentGhosts(getListOfGhosts());
                UpdatePacman(elapsed);
                map.Update(elapsed, getPacmanPosition());
            }
            base.Update(gameTime);
        }


        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here
            sprite.Begin(SpriteBlendMode.AlphaBlend);

            map.Render(ref sprite);
            RenderPacman(ref sprite);
            RenderGhosts(ref sprite);
            sprite.End();

            base.Draw(gameTime);
        }

        private bool wasEscapePressed()
        {
            if (KeyPress.KeyPressGame.KeyPressedThisFrame(Keys.Escape))
            {
                this.Exit();
                return true;
            }
            return false;
        }
        #endregion

        #region pacman methods
        public void UpdatePacman(float elapsed)
        {
            CheckKeys();
            if (!playDeathAnimation)
            {
                CheckCollisionWithGhosts();
                if (currentDirection == Direction.NORTH)
                {
                    spriteY -= 65.0f * elapsed;
                    currentTexture = pacmanUp;
                    if (adjustDistance)
                    {
                        spriteY -= length;
                        adjustDistance = false;
                    }

                }
                else if (currentDirection == Direction.SOUTH)
                {
                    spriteY += 65.0f * elapsed;
                    currentTexture = pacmanDown;
                    if (adjustDistance)
                    {
                        spriteY += length;
                        adjustDistance = false;
                    }
                }
                else if (currentDirection == Direction.WEST)
                {

                    spriteX -= 65.0f * elapsed;
                    currentTexture = pacmanLeft;
                    if (adjustDistance)
                    {
                        spriteX -= length;
                        adjustDistance = false;
                    }

                }
                else if (currentDirection == Direction.EAST)
                {

                    spriteX += 65.0f * elapsed;
                    currentTexture = pacmanRight;
                    if (adjustDistance)
                    {
                        spriteX += length;
                        adjustDistance = false;
                    }

                }
                else if (currentDirection == Direction.STOP)
                {
                    //Do nothing
                    Waypoint tmp = currentWaypoints[nextWaypoint];
                    spriteX = tmp.Position.X;
                    spriteY = tmp.Position.Y;
                }
                totalTime += elapsed;
                if (totalTime > 0.25f)
                {
                    currentKey = Keys.R;
                    currentX += 32;
                    totalTime = 0.0f;
                    if (currentX > 64)
                        currentX = 0;
                }
                CheckNextWaypoint();
            }
            else
            {
                //Update animation time
                frameTime -= elapsed;
            }


        }
        public void RenderPacman(ref SpriteBatch sprite)
        {
            if (!playDeathAnimation)
            {
                sprite.Draw(currentTexture, new Vector2(spriteX, spriteY), new Rectangle(currentX, 0, 32, 32), Color.White, 0.0f, new Vector2(16.0f, 16.0f), 1.0f, SpriteEffects.None, 0.1f);
            }
            else
            {
                //PlayAnimation
                if (frameTime < 0.0f && currentDeathFrame < 3)
                {
                    frameTime = 0.25f;
                    currentDeathFrame++;
                }

                sprite.Draw(deathFrames, new Vector2(spriteX, spriteY), new Rectangle(currentDeathFrame * 32, 0, 32, 32), Color.White, 0.0f, new Vector2(16.0f, 16.0f), 1.0f, SpriteEffects.None, 0.1f);
                if (currentDeathFrame == 3 && frameTime < 0.0f)
                {
                    frameTime = 0.25f;
                    currentDeathFrame = 0;
                    playDeathAnimation = false;
                    Waypoint tmp = currentWaypoints[0];
                    spriteX = tmp.Position.X;
                    spriteY = tmp.Position.Y;
                    nextWaypoint = tmp.Left;
                    prevWaypoint = tmp.Right;
                    currentDirection = Direction.WEST;

                }
            }


        }
        //Checks Key Presses when Pacman has not yet arrived to a junction
        private void CheckKeys()
        {
            if (KeyPress.KeyPressGame.KeyPressedThisFrame(Keys.Up) && 
                currentDirection == Direction.SOUTH)
            {
                int tmp = nextWaypoint;
                nextWaypoint = prevWaypoint;
                prevWaypoint = tmp;
                currentDirection = Direction.NORTH;
            }
            else if (KeyPress.KeyPressGame.KeyPressedThisFrame(Keys.Down) && 
                currentDirection == Direction.NORTH)
            {
                int tmp = nextWaypoint;
                nextWaypoint = prevWaypoint;
                prevWaypoint = tmp;
                currentDirection = Direction.SOUTH;
            }
            else if (KeyPress.KeyPressGame.KeyPressedThisFrame(Keys.Left) && 
                currentDirection == Direction.EAST)
            {
                int tmp = nextWaypoint;
                nextWaypoint = prevWaypoint;
                prevWaypoint = tmp;
                currentDirection = Direction.WEST;
            }
            else if (KeyPress.KeyPressGame.KeyPressedThisFrame(Keys.Right) && 
                currentDirection == Direction.WEST)
            {
                int tmp = nextWaypoint;
                nextWaypoint = prevWaypoint;
                prevWaypoint = tmp;
                currentDirection = Direction.EAST;
            }
            Keys tmpKey = currentKey;
            currentKey = getKeyPress();
            if (currentKey == Keys.R)
                currentKey = tmpKey;
        }
        private void CheckNextWaypoint()
        {
            Waypoint tmp = currentWaypoints[nextWaypoint];
            Vector2 distance = (tmp.Position - new Vector2(spriteX, spriteY));
            length = distance.Length();
            if (getDirectionToWaypoint(distance) == currentDirection && 
                currentDirection != Direction.STOP)
            {
                adjustDistance = false;
                return;
            }
            else
            {
                adjustDistance = true;
                switch (currentKey)
                {
                    case Keys.Up:
                        if (tmp.Up < 90)
                        {
                            int way = nextWaypoint;
                            nextWaypoint = tmp.Up;
                            prevWaypoint = way;
                            currentDirection = Direction.NORTH;
                        }
                        else
                            currentDirection = Direction.STOP;
                        break;
                    case Keys.Down:
                        if (tmp.Up < 90)
                        {
                            int way = nextWaypoint;
                            nextWaypoint = tmp.Up;
                            prevWaypoint = way;
                            currentDirection = Direction.SOUTH;
                        }
                        else
                            currentDirection = Direction.STOP;
                        break;
                    case Keys.Left:
                        if (tmp.Left < 90)
                        {
                            int way = nextWaypoint;
                            nextWaypoint = tmp.Left;
                            prevWaypoint = way;
                            currentDirection = Direction.WEST;
                        }
                        else
                            currentDirection = Direction.STOP;
                        break;
                    case Keys.Right:
                        if (tmp.Right < 90)
                        {
                            int way = nextWaypoint;
                            nextWaypoint = tmp.Right;
                            prevWaypoint = way;
                            currentDirection = Direction.EAST;
                        }
                        else
                            currentDirection = Direction.STOP;
                        break;
                    default:
                        switch (currentDirection)
                        {
                            case Direction.WEST:
                                if (tmp.Left < 99)
                                {
                                    int way = nextWaypoint;
                                    nextWaypoint = tmp.Left;
                                    prevWaypoint = way;
                                }
                                else
                                    currentDirection = Direction.STOP;
                                break;
                            case Direction.NORTH:
                                if (tmp.Up < 99)
                                {
                                    int way = nextWaypoint;
                                    nextWaypoint = tmp.Up;
                                    prevWaypoint = way;
                                }
                                else
                                    currentDirection = Direction.STOP;
                                break;
                            case Direction.EAST:
                                if (tmp.Right < 99)
                                {
                                    int way = nextWaypoint;
                                    nextWaypoint = tmp.Right;
                                    prevWaypoint = way;
                                }
                                else
                                    currentDirection = Direction.STOP;
                                break;
                            case Direction.SOUTH:
                                if (tmp.Up < 99)
                                {
                                    int way = nextWaypoint;
                                    nextWaypoint = tmp.Up;
                                    prevWaypoint = way;
                                }
                                else
                                    currentDirection = Direction.STOP;
                                break;
                        }//End Switch
                        break;
                }//End switch;
            }
        }
        void setCurrentPacmanWaypoints(List<Waypoint> list)
        {
            currentWaypoints = list;
            if (currentWaypoints != null)
            {
                Waypoint tmp = (Waypoint)list[0];
                spriteX = tmp.Position.X;
                spriteY = tmp.Position.Y;
                nextWaypoint = tmp.Left;
                prevWaypoint = tmp.Right;
                currentDirection = Direction.WEST;
            }
        }
        private Keys getKeyPress()
        {
            if (KeyPress.KeyPressGame.KeyPressedThisFrame(Keys.Up))
                return Keys.Up;
            if (KeyPress.KeyPressGame.KeyPressedThisFrame(Keys.Down))
                return Keys.Down;
            if (KeyPress.KeyPressGame.KeyPressedThisFrame(Keys.Left))
                return Keys.Left;
            if (KeyPress.KeyPressGame.KeyPressedThisFrame(Keys.Right))
                return Keys.Right;
            return Keys.R;
        }
        private Direction getDirectionToWaypoint(Vector2 vec)
        {
            vec.Normalize();
            if (vec.X < 0.45f && vec.X > -0.45f)
            {
                vec.X = 0.0f;
            }
            if (vec.X < -0.5f)
            {
                vec.X = -1.0f;
                vec.Y = 0.0f;
            }
            if (vec.X > 0.5)
            {
                vec.X = 1.0f;
                vec.Y = 0.0f;
            }
            if (vec.Y < 0.45f && vec.Y > -0.45f)
            {
                vec.Y = 0.0f;
            }
            if (vec.Y > 0.5)
            {
                vec.Y = 1.0f;
                vec.X = 0.0f;
            }
            if (vec.Y < -0.5f)
            {
                vec.Y = -1.0f;
                vec.X = 0.0f;
            }
            if (vec.X == 1.0f)
                return Direction.EAST;
            else if (vec.X == -1.0f)
                return Direction.WEST;
            else if (vec.Y == 1.0f)
                return Direction.NORTH;
            else if (vec.Y == -1.0f)
                return Direction.SOUTH;
            return Direction.STOP;
        }
        public Vector2 getPacmanPosition()
        {
            return new Vector2(spriteX, spriteY);
        }
        void setCurrentGhosts(List<Ghost> list)
        {
            currentGhostPositions = list;
        }
        private void CheckCollisionWithGhosts()
        {
            if (currentGhostPositions != null)
            {
                foreach (Ghost a in currentGhostPositions)
                {
                    //Calculate the distance to the ghost
                    Vector2 distance = a.Position - new Vector2(spriteX, spriteY);
                    if (distance.Length() < 32)
                    {
                        //Collision
                        playDeathAnimation = true;
                        lives--;
                        break;
                    }
                }//End for each
            }//End if
        }//End function CheckCollisionWithGhosts
        #endregion

        #region ghost methods
        public void UpdateGhosts(float elapsed, Vector2 pacPosition)
        {
            foreach (Ghost a in ghostList)
            {

                //If not at waypoint just update position and move to next
                //Ghost
                if (a.currentDirection == getDirectionToWaypoint(a.Position, a.nextWayPoint) && a.currentDirection != Direction.STOP)
                {
                    normalUpdate(a, elapsed);
                }
                else
                {
                   // sw.Write(a.nextWayPoint);
                    tmpWay = (Waypoint)wayPointList[a.nextWayPoint];
                    Vector2 length = (tmpWay.Position - a.Position);
                    float distance = length.Length();
                    int value;
                    value = rand.Next(99);
                    //This is in here so the AI can be made random between a wise and a random
                    //decision
                    if (value < 100)
                    {

                        //First check to see if it can continue in the same direction if the random number is between 1 and 25
                        if (value < 50)
                        {
                            //Check to see if ghost can continue in same direction
                            switch (a.currentDirection)
                            {
                                case Direction.EAST:
                                    if (tmpWay.Right < wayPointList.Count - 1)
                                    {
                                        int tmp = a.nextWayPoint;
                                        a.nextWayPoint = tmpWay.Right;
                                        a.prevWayPoint = tmp;

                                        a.Position = new Vector2(tmpWay.Position.X + distance + (a.speed * elapsed), tmpWay.Position.Y);
                                        continue;
                                    }
                                    break;
                                case Direction.NORTH:
                                    if (tmpWay.Up < wayPointList.Count - 1)
                                    {
                                        int tmp = a.nextWayPoint;
                                        a.nextWayPoint = tmpWay.Up;
                                        a.prevWayPoint = tmp;
                                        a.Position = new Vector2(tmpWay.Position.X, tmpWay.Position.Y - (distance + (a.speed * elapsed)));
                                        continue;
                                    }
                                    break;
                                case Direction.SOUTH:
                                    if (tmpWay.Up < wayPointList.Count - 1)
                                    {
                                        int tmp = a.nextWayPoint;
                                        a.nextWayPoint = tmpWay.Up;
                                        a.prevWayPoint = tmp;
                                        a.Position = new Vector2(tmpWay.Position.X, tmpWay.Position.Y + distance + (a.speed * elapsed));
                                        continue;
                                    }
                                    break;
                                case Direction.WEST:
                                    if (tmpWay.Left < wayPointList.Count - 1)
                                    {
                                        int tmp = a.nextWayPoint;
                                        a.nextWayPoint = tmpWay.Left;
                                        a.prevWayPoint = tmp;
                                        a.Position = new Vector2(tmpWay.Position.X - (distance + (a.speed * elapsed)), tmpWay.Position.Y);
                                        continue;
                                    }
                                    break;
                            }//End switch
                        }
                        //If not move in a random direction
                        //Random Move
                        value = rand.Next(0, 3);
                        switch (value)
                        {
                            case 0://Move WEST
                                if (tmpWay.Left < 90)
                                {
                                    int tmp = a.nextWayPoint;
                                    a.nextWayPoint = tmpWay.Left;
                                    a.prevWayPoint = tmp;
                                    a.currentDirection = Direction.WEST;
                                }
                                else //Pick another direction
                                {
                                    a.currentDirection = getNextDirection(a.currentDirection);
                                    while (!tryNextDirection(a, a.currentDirection))
                                    {
                                        a.currentDirection = getNextDirection(a.currentDirection);
                                    }
                                    setNewPoints(a);
                                }
                                break;
                            case 1: //EAST
                                if (tmpWay.Right < 90)
                                {
                                    int tmp = a.nextWayPoint;
                                    a.nextWayPoint = tmpWay.Right;
                                    a.prevWayPoint = tmp;
                                    a.currentDirection = Direction.EAST;
                                }
                                else
                                {
                                    a.currentDirection = getNextDirection(a.currentDirection);
                                    while (!tryNextDirection(a, a.currentDirection))
                                    {
                                        a.currentDirection = getNextDirection(a.currentDirection);
                                    }
                                    setNewPoints(a);
                                }
                                break;
                            case 2:  //South
                                if (tmpWay.Up < 90)
                                {
                                    int tmp = a.nextWayPoint;
                                    a.nextWayPoint = tmpWay.Up;
                                    a.prevWayPoint = tmp;
                                    a.currentDirection = Direction.SOUTH;
                                }
                                else
                                {
                                    a.currentDirection = getNextDirection(a.currentDirection);
                                    while (!tryNextDirection(a, a.currentDirection))
                                    {
                                        a.currentDirection = getNextDirection(a.currentDirection);
                                    }
                                    setNewPoints(a);
                                }
                                break;
                            case 3:
                                if (tmpWay.Up < 90)
                                {
                                    int tmp = a.nextWayPoint;
                                    a.nextWayPoint = tmpWay.Up;
                                    a.prevWayPoint = tmp;
                                    a.currentDirection = Direction.NORTH;
                                }
                                else
                                {
                                    a.currentDirection = getNextDirection(a.currentDirection);
                                    while (!tryNextDirection(a, a.currentDirection))
                                    {
                                        a.currentDirection = getNextDirection(a.currentDirection);
                                    }
                                    setNewPoints(a);
                                }
                                break;
                        }//End Switch


                    }//End if

                }
            }

        }
        public void RenderGhosts(ref SpriteBatch sprite)
        {
            int x = 0;
            int y = 0;
            foreach (Ghost a in ghostList)
            {
                x = (int)a.Position.X;
                y = (int)a.Position.Y;
                sprite.Draw(a.Texture, new Rectangle(x, y, 32, 32), Color.White);
            }//End foreach

        }
        void setWaypoints(List<Waypoint> list)
        {
            wayPointList = list;

            foreach (Ghost a in ghostList)
            {
                tmpWay = wayPointList[a.prevWayPoint];
                a.Position = tmpWay.Position;
            }
        }
        //===========================================
        //Returns Direction of Next Waypoint
        //===========================================
        private Direction getDirectionToWaypoint(Vector2 pos, int nextWayIndex)
        {
            if (nextWayIndex > wayPointList.Count - 1)
                return Direction.STOP;
            tmpWay = wayPointList[nextWayIndex];
            Vector2 vec = tmpWay.Position - pos;
            vec.Normalize();
            if (vec.X < 0.45f && vec.X > -0.45f)
            {
                vec.X = 0.0f;
            }
            if (vec.X < -0.5f)
            {
                vec.X = -1.0f;
                vec.Y = 0.0f;
            }
            if (vec.X > 0.5)
            {
                vec.X = 1.0f;
                vec.Y = 0.0f;
            }
            if (vec.Y < 0.45f && vec.Y > -0.45f)
            {
                vec.Y = 0.0f;
            }
            if (vec.Y > 0.5)
            {
                vec.Y = 1.0f;
                vec.X = 0.0f;
            }
            if (vec.Y < -0.5f)
            {
                vec.Y = -1.0f;
                vec.X = 0.0f;
            }
            if (vec.X == 1.0f)
                return Direction.EAST;
            else if (vec.X == -1.0f)
                return Direction.WEST;
            else if (vec.Y == 1.0f)
                return Direction.SOUTH;
            else if (vec.Y == -1.0f)
                return Direction.NORTH;
            return Direction.STOP;
        }
        private Direction reverseDirection(Direction dir)
        {
            if (dir == Direction.EAST)
                return Direction.WEST;
            if (dir == Direction.NORTH)
                return Direction.SOUTH;
            if (dir == Direction.SOUTH)
                return Direction.NORTH;
            if (dir == Direction.WEST)
                return Direction.EAST;

            return Direction.STOP;
        }
        private Direction getRelativeDirection(Vector2 ghost, Vector2 pac)
        {
            //Calculate the greatest distance between pacman and ghost
            float Ydifference = pac.Y - ghost.Y;
            float Xdifference = pac.X - ghost.X;
            if (pac.Y > ghost.Y && Ydifference > Xdifference)
                return Direction.SOUTH;
            if (pac.Y < ghost.Y && Ydifference > Xdifference)
                return Direction.NORTH;
            if (pac.X > ghost.X && Xdifference > Ydifference)
                return Direction.EAST;
            if (pac.X < ghost.X && Xdifference > Ydifference)
                return Direction.WEST;
            return Direction.WEST;
        }
        private Direction getNextDirection(Direction dir)
        {
            if (dir == Direction.WEST)
                return Direction.NORTH;
            if (dir == Direction.NORTH)
                return Direction.EAST;
            if (dir == Direction.EAST)
                return Direction.SOUTH;
            if (dir == Direction.SOUTH)
                return Direction.WEST;
            if (dir == Direction.STOP)
                return Direction.EAST;

            return Direction.STOP;
        }
        private bool tryNextDirection(Ghost ghost, Direction dir)
        {
            Waypoint tmp = (Waypoint)wayPointList[ghost.nextWayPoint];
            switch (dir)
            {
                case Direction.EAST:
                    if (tmp.Right < wayPointList.Count - 1)
                        return true;
                    break;
                case Direction.NORTH:
                    if (tmp.Up < wayPointList.Count - 1)
                        return true;
                    break;
                case Direction.SOUTH:
                    if (tmp.Up < wayPointList.Count - 1)
                        return true;
                    break;
                case Direction.WEST:
                    if (tmp.Left < wayPointList.Count - 1)
                        return true;
                    break;
            }
            return false;
        }
        private void setNewPoints(Ghost ghost)
        {
            if (ghost.nextWayPoint > wayPointList.Count - 1 || ghost.nextWayPoint < 0)
                return;
            Waypoint tmp = (Waypoint)wayPointList[ghost.nextWayPoint];
            int prev;
            switch (ghost.currentDirection)
            {
                case Direction.EAST:
                    prev = ghost.nextWayPoint;
                    ghost.nextWayPoint = tmp.Right;
                    ghost.prevWayPoint = prev;
                    break;
                case Direction.NORTH:
                    prev = ghost.nextWayPoint;
                    ghost.nextWayPoint = tmp.Up;
                    ghost.prevWayPoint = prev;
                    break;
                case Direction.SOUTH:
                    prev = ghost.nextWayPoint;
                    ghost.nextWayPoint = tmp.Up;
                    ghost.prevWayPoint = prev;
                    break;
                case Direction.WEST:
                    prev = ghost.nextWayPoint;
                    ghost.nextWayPoint = tmp.Left;
                    ghost.prevWayPoint = prev;
                    break;
            }
        }
        private void normalUpdate(Ghost a, float elapsed)
        {
            //Update Direction
            if (a.currentDirection == Direction.EAST)
            {
                a.Position = new Vector2(a.Position.X + (a.speed * elapsed), a.Position.Y);
            }
            if (a.currentDirection == Direction.NORTH)
            {
                a.Position = new Vector2(a.Position.X, a.Position.Y - (a.speed * elapsed));
            }
            if (a.currentDirection == Direction.SOUTH)
            {
                a.Position = new Vector2(a.Position.X, a.Position.Y + (a.speed * elapsed));
            }
            if (a.currentDirection == Direction.WEST)
            {
                a.Position = new Vector2(a.Position.X - (a.speed * elapsed), a.Position.Y);
            }
        }
        List<Ghost> getListOfGhosts()
        {
            return ghostList;
        }
           
        #endregion
    }
}